package com.bt.dmdb.helpers;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
//import org.apache.commons.collections.CollectionUtils;
import org.hibernate.SQLQuery;
import org.hibernate.type.Type;
//import com.bt.dmdb.bean.Filter;
//import com.bt.dmdb.bean.SearchParam;
import com.bt.dmdb.dto.CustomerDto;
import com.bt.dmdb.enums.Constraint;
import com.bt.dmdb.enums.Match;
import com.bt.dmdb.enums.OrderBy;

public class SqlBuilder {/*

    private String query;
    private boolean where;

    private SqlBuilder(String query) {
        this.query = query;
    }

    public static SQLQuery addOrderScalar(SQLQuery query) {
    	return addScalar(query,OrderDto.SCALAR_MAP);
    }
    public static SQLQuery addScalar(SQLQuery query, Map<? super Type, String[]> scalars) {
    	for (Entry<? super Type, String[]> scalarEntry : scalars.entrySet()) {
    		Type type = (Type) scalarEntry.getKey();
    		for (String scalar : scalarEntry.getValue()) {
    			query.addScalar(scalar, type);
    		}
    	}
    	return query;
    }

    public static SqlBuilder create(String query, SearchParam searchParam) {
        SqlBuilder sqlBuilder = new SqlBuilder(query);
        return sqlBuilder.addMandatoryFilters(searchParam).addOptionalFilters(searchParam).orderBy(searchParam);
    }

    public static SqlBuilder generate(String query, SearchParam searchParam) {
        SqlBuilder sqlBuilder = new SqlBuilder(query);
        return sqlBuilder.addMandatoryFilters(searchParam).addOptionalFilters(searchParam);
    }

    public static String listToIn(List<String> params){
    	String in =" ";
    	for(String param: params){
    		in = in+"'"+param+"',";
    	}
    	return in.substring(0, in.length()-1);
    }

    public static String setIn(String query, List<String> params){
    	String in =listToIn(params);
    	query = query+"("+in+")";
    	return query;
    }

    public static String orderBy(String query, String col, String order){
        query = query+" order by "+col+" "+order;
        return query;
    }

    public SqlBuilder addOptionalFilters(SearchParam searchParam) {
        return Match.getMatch(searchParam.getMatch()).addFilters(searchParam.getFilters(), this);
    }

    public SqlBuilder addMandatoryFilters(SearchParam searchParam) {
        List<Map<String, String>> mandList = searchParam.getMandatoryFilters();
        if (CollectionUtils.isNotEmpty(mandList)) {
            for (Map<String, String> filterMap : mandList) {
                Filter filter = new Filter(filterMap, "SqlFilter");
                Constraint constraint = filter.getConstraint();
                String condition = constraint.getFilter(filter.getProperty(), filter.getValue().toString(), filter.isIgnoreCase());
                query = query + " and " + condition;
            }
        }
        return this;
    }

    public SqlBuilder orderBy(SearchParam searchParam) {
        return orderBy(searchParam.getOrder(), searchParam.getOrderCol());
    }

    public SqlBuilder orderBy(String order, String orderCol) {
        OrderBy.valueOf(order).addOrder(this, orderCol);
        return this;
    }

    public String countSql() {
        return new StringBuilder("select count(*) from (").append(query).append(")").toString();
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public boolean hasWhere() {
        return where;
    }

    public void setWhere(boolean where) {
        this.where = where;
    }
*/}
